jQuery(document).ready(function($) {
    var cookie = JSON.parse($.cookie('bjou_school'));
	$.ajax({
		type: 'GET',
		url: 'api/v1/check'

	}).done(function (data, textStatus, jqXHR) {

	}).fail(function (jqXHR, textStatus, errorThrown) {
        window.location = '../index.html';
	});
    $('#welcome').html("欢迎您," + cookie.schoolName)
	$('#logout').on('click', function (event) {
		event.preventDefault();
		$.ajax({
			data: {},
			timeout: 1000,
			type: 'POST',
			url: '../logout'

		}).done(function(data, textStatus, jqXHR) {
			window.location = '../index.html';
		}).fail(function(jqXHR, textStatus, errorThrown) {
		    window.location = '../index.html';
		});
	});


});

var cookie = JSON.parse($.cookie('bjou_school'));
var getSchoolEnrollmentUrl = 'api/v1/enrollment';
var getSchoolEnrollmentOptions = {
    elem: '#SchoolEnrollment',
    url: getSchoolEnrollmentUrl,
    method: 'POST',
    contentType: 'application/json',
    id: 'schoolEnrollmentTable',
    page: true,
    limit: 20,
    loading: true,
    even: true,
    cols: [[
        {type:'numbers', title:'序号', width: 50, sort: true}
        , {field:'rxnd', hide:true}
        , {field:'rxxqdm', hide:true}
        , {field:'nj', title:'年级'}
        , {field:'xxdm', title:'学校代码'}
        , {field:'xxmc', title:'学校名称'}
        , {field:'xslbdm', hide:true}
        , {field:'xslbmc', title:'学生类别'}
        , {field:'zydm', hide:true}
        , {field:'zymc', title:'专业名称'}
        , {field:'count', title:'人数'}
        , {fixed:'right', width:50, align:'center', toolbar:'#EnrollmentDetails'}
    ]],
    request: {
        limitName: 'size'
    },
    response: {
        statusName: null,
        statusCode: null,
        countName: 'totalElements',
        dataName: 'content'
    },
    where: {
        'schoolID': cookie.schoolID
    }
};
layui.config({
    base: '../layui/plugins/'
})
layui.use(['form', 'table', 'multiSelect', 'element'], function(){
    var $ = layui.jquery
    ,form = layui.form
    ,table = layui.table
    ,element = layui.element
    ,multiSelect = layui.multiSelect;

    var zylb_htmls = '<option value="">请选择</option>';
    $.ajax({
        data: JSON.stringify({schoolID: cookie.schoolID}),
		type: 'POST',
		dataType: "json",
        contentType: "application/json;charset=utf-8",
		url: 'api/v1/enrollment/zylb'
	}).done(function (data, textStatus, jqXHR) {
        data
        .sort(function (a, b) {
         return a.zymc > b.zymc ? 1 : -1;
        })
        .forEach(function(item) {
            zylb_htmls += '<option value="'+item.zydm+'">'+item.zymc+'</option>';
        })
        $("#zylb").html(zylb_htmls);
        form.render(null, 'app-content-list');
        multiSelect.init()
	}).fail(function (jqXHR, textStatus, errorThrown) {
	});

	var xslb_htmls = '<option value="">请选择</option>';
    $.ajax({
        data: JSON.stringify({schoolID: cookie.schoolID}),
		type: 'POST',
		dataType: "json",
        contentType: "application/json;charset=utf-8",
        url: 'api/v1/enrollment/xslb'
    }).done(function (data, textStatus, jqXHR) {
        data
        .sort(function (a, b) {
            return a.xslbmc > b.xslbmc ? 1 : -1;
        })
        .forEach(function(item) {
            xslb_htmls += '<option value="'+item.xslbdm+'">'+item.xslbmc+'</option>';
        })
        $("#xslb").html(xslb_htmls);
        form.render(null, 'app-content-list');
        multiSelect.init()
    }).fail(function (jqXHR, textStatus, errorThrown) {
    });

    var ndxq_htmls = '<option value="">请选择</option>';
    $.ajax({
        data: JSON.stringify({schoolID: cookie.schoolID}),
		type: 'POST',
		dataType: "json",
        contentType: "application/json;charset=utf-8",
        url: 'api/v1/enrollment/ndxq'
    }).done(function (data, textStatus, jqXHR) {
        data
        .sort(function (a, b) {
            return (a.rxnd + a.rxxq) < (b.rxnd + b.rxxq) ? 1 : -1;
        })
        .forEach(function(item) {
            ndxq_htmls += '<option value="'+item.rxnd +'#'+item.rxxqdm+'">'+item.rxnd + item.rxxqmc+'</option>';
        })
        $("#ndxq").html(ndxq_htmls);
        form.render(null, 'app-content-list');
        multiSelect.init()
    }).fail(function (jqXHR, textStatus, errorThrown) {
    });

    form.on('submit(LAY-app-contlist-search)', function(data){
        var zylb_vals = [];
        $("select[name='zylb'] option:selected").each(function() {
            zylb_vals.push($(this).val());
        })
        var xslb_vals = [];
        $("select[name='xslb'] option:selected").each(function() {
            xslb_vals.push($(this).val());
        })
        var ndxq_vals = [];
        $("select[name='ndxq'] option:selected").each(function() {
            ndxq_vals.push($(this).val());
        })
        var options = {
                'zylb': zylb_vals.join(","),
                'xslb': xslb_vals.join(","),
                'ndxq': ndxq_vals.join(","),
                'schoolID': cookie.schoolID
            };
        table.reload('schoolEnrollmentTable', {
            where: options,
            page: {curr: 1}
        })
    });
    table.on('tool(SchoolEnrollment)', function(obj){
        var data = obj.data; //获得当前行数据
        var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
        var enrollmentDetailsOptions = {
            xxdm: data.xxdm,
            rxnd: data.rxnd,
            rxxq: data.rxxqdm,
            xslbdm: data.xslbdm,
            zydm: data.zydm
        }
        if(layEvent === 'detail'){ //查看
            $.cookie('bjou_school_enrollment_details', JSON.stringify(enrollmentDetailsOptions));
            window.open('enrollment_details.html');
        }
    });
    table.render(getSchoolEnrollmentOptions);

    $('.layui-btn').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

});